package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.combat.Option;
import com.itheima.domain.combat.QuestionSoul;
import com.itheima.domain.combat.Questionnaire;
import com.itheima.domain.combatVo.OptionVo;
import com.itheima.domain.combatVo.QuestionSoulVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.db.OptionService;
import com.itheima.service.db.QuestionSoulService;
import com.itheima.service.db.QuestionnaireService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SoulCombatManager {
    @Reference
    private QuestionSoulService questionSoulService;
    @Reference
    private QuestionnaireService questionnaireService;
    @Reference
    private OptionService optionService;

    //后台添加灵魂测试的问题
    public void addQuestion(String questionnaireLevel, List<String> optionContextAndScoreList, String questionContext) {
        int i = 1;
        Questionnaire questionnaire = questionnaireService.findByLevel(questionnaireLevel);
        //创建问题的对象
        QuestionSoul questionSoul = new QuestionSoul();
        questionSoul.setQuestion(questionContext);
        questionSoul.setQuestionnaireId(questionnaire.getId());
        Integer questionId = questionSoulService.addQuestion(questionSoul);
        //添加对应问题的选项
        if (CollectionUtil.isNotEmpty(optionContextAndScoreList)) {
            for (String s : optionContextAndScoreList) {
                String[] split = s.split("#");
                if (split != null) {
                    //创建选项的对象
                    Option option = new Option();
                    option.setQuestionSoulId(questionId);
                    option.setScore(Integer.parseInt(split[1]));
                    option.setOptionContext(split[0]);
                    optionService.saveOption(option);
                }
            }
        }
    }


    //删除题目
    public void deleteQuestion(String questionId) {
        questionSoulService.dedeleteQuestion(Integer.parseInt(questionId));
        //根据题目的id删除选项
        optionService.deleteQuestion(questionId);
    }


    //查询所有的问题
    public PageBeanVo findQuestionSoul(Integer pageNum, Integer pageSize, String questionnaireId) {
        //查询所有的题目
        PageBeanVo pageBeanVo = questionSoulService.findQuestion(pageNum, pageSize, questionnaireId);
        List<QuestionSoul> questionList = (List<QuestionSoul>) pageBeanVo.getItems();
        //创建返回问题的vo对象集合
        List<QuestionSoulVo> voList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(questionList)) {
            for (QuestionSoul questionSoul : questionList) {
                //创建vo对象
                QuestionSoulVo questionSoulVo = new QuestionSoulVo();
                questionSoulVo.setId(questionSoul.getId().toString());
                questionSoulVo.setQuestion(questionSoul.getQuestion());
                List<Option> optionById = optionService.findOptionById(questionSoul.getId());
                List<OptionVo> optionVoList = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(optionById)) {
                    for (Option option : optionById) {
                        OptionVo optionVo = new OptionVo();
                        optionVo.setId(option.getId().toString());
                        optionVo.setOption(option.getOptionContext() + "#" + option.getScore());
                        optionVoList.add(optionVo);
                    }
                }

                questionSoulVo.setOptions(optionVoList);
                voList.add(questionSoulVo);
            }
        }
        pageBeanVo.setItems(voList);
        return pageBeanVo;
    }

    //修改题目
    public void updateQuestion(String questionId, List<Map> optionContextAndScoreList, String questionContext) {
        int i = 1;
        QuestionSoul questionSoul = questionSoulService.findQuestionById(questionId);
        questionSoul.setQuestion(questionContext);
        questionSoulService.updateQuestionSoul(questionSoul);
        List<Option> optionById = optionService.findOptionById(Integer.parseInt(questionId));
        //创建一个选项内容的集合
        List<String> optionContextList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(optionContextAndScoreList)) {
            for (Map map : optionContextAndScoreList) {
                String s = (String) map.get("answer" + (i++));
                if (s != null) {
                    String[] split = s.split("#");
                    if (split != null) {
                        optionContextList.add(split[0]);
                    }
                }
            }
        }
        optionService.updateOption(optionContextList, questionId);
    }
}
